package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Job;
import com.yixin.hospital.entity.domain.JobTitle;
import com.yixin.hospital.entity.dto.req.job.JobAddReqDTO;
import com.yixin.hospital.entity.dto.req.job.JobEditReqDTO;
import com.yixin.hospital.entity.vo.JobVo;
import com.yixin.hospital.service.JobService;
import com.yixin.hospital.mapper.JobMapper;
import com.yixin.hospital.service.JobTitleService;
import com.yixin.hospital.utils.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 
* @description 针对表【job(职业表)】的数据库操作Service实现
* @createDate 2024-03-24 12:58:03
*/
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job>
    implements JobService{

    @Resource
    JobTitleService jobTitleService;

    @Override
    public ApiResult treeList() {
        List<Job> list = this.list();
        List<JobVo> jobVoList = BeanUtil.copyToList(list, JobVo.class);
        List<JobVo> jobTitleVoList = new ArrayList<>();
        for (JobVo jobVo : jobVoList) {
            jobVo.setLevel(1);
            jobVo.setPid(0L);
            // job title
            LambdaQueryWrapper<JobTitle> queryWrapper = new LambdaQueryWrapper<>();
            List<JobTitle> jobTitles = jobTitleService.list(queryWrapper.eq(JobTitle::getJobId, jobVo.getId()));
            if (CollectionUtils.isEmpty(jobTitles)) continue;
            List<JobVo> tmpList = BeanUtil.copyToList(jobTitles, JobVo.class);
            for (JobVo jobTitleVo : tmpList) {
                jobTitleVo.setLevel(2);
                jobTitleVo.setPid(jobVo.getId());
            }
            jobTitleVoList.addAll(tmpList);
        }
        jobVoList.addAll(jobTitleVoList);
        TreeNodeConfig nodeConfig = new TreeNodeConfig();
        List<Tree<Integer>> treeList = TreeUtil.build(jobVoList, 0, nodeConfig,
                (node, tree) -> {
                    tree.setId(node.getId().intValue());
                    tree.setParentId(node.getPid().intValue());
                    tree.setName(node.getName());
                    tree.putExtra("pid", node.getPid());
                    tree.putExtra("level", node.getLevel());
                });
        return ApiResult.success(CollectionUtil.isEmpty(treeList) ? new ArrayList<>() : treeList);
    }

    @Override
    public ApiResult add(JobAddReqDTO jobAddReqDTO, String userName) {
        Job job = new Job();
        job.setName(jobAddReqDTO.getName());
        job.setCreatedBy(userName);
        job.setUpdatedBy(userName);
        this.save(job);
        return ApiResult.success();
    }

    @Override
    public ApiResult edit(JobEditReqDTO jobEditReqDTO, String userName) {
        Job job = new Job();
        job.setId(jobEditReqDTO.getId());
        job.setName(jobEditReqDTO.getName());
        job.setUpdatedBy(userName);
        this.updateById(job);
        return ApiResult.success();
    }
}




